package com.nanrui.mutualinductor.ui.page;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.batman.baselibrary.base.BaseActivity;
import com.batman.baselibrary.db.AppDatabase;
import com.batman.baselibrary.db.RtBean;
import com.batman.baselibrary.utils.TipDialogUtils;
import com.batman.baselibrary.utils.ToastUtils;
import com.google.gson.Gson;
import com.nanrui.mutualinductor.R;
import com.nanrui.mutualinductor.data.CTCheckData;
import com.nanrui.mutualinductor.data.CheckData;
import com.nanrui.mutualinductor.domains.CurrentTransformerViewInfo;
import com.nanrui.mutualinductor.domains.VoltageTransformerViewInfo;
import com.nanrui.mutualinductor.ui.widget.NoScrollViewPager;
import com.raizlabs.android.dbflow.config.FlowManager;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper;
import com.raizlabs.android.dbflow.structure.database.transaction.ProcessModelTransaction;
import com.raizlabs.android.dbflow.structure.database.transaction.Transaction;
import com.ui.widget.UINavigationView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class HistoryActivity extends BaseActivity {

    @BindView(R.id.uinv)
    UINavigationView mUinv;
    @BindView(R.id.tv_num)
    TextView mTvNum;

    @BindView(R.id.tv_date)
    TextView mTvDate;
    @BindView(R.id.btn_pre)
    Button mBtnPre;
    @BindView(R.id.btn_next)
    Button mBtnNext;
    @BindView(R.id.btn_delete)
    Button mBtnDelete;
    @BindView(R.id.btn_delete_all)
    Button mBtnDeleteAll;
    @BindView(R.id.btn_exit)
    Button mBtnExit;

    @BindView(R.id.ll_no_data)
    LinearLayout mLLNodata;

    @BindView(R.id.tv_content)
    TextView tvContent;

    @BindView(R.id.view_pager)
    NoScrollViewPager mViewPager;

    private ViewPagerAdapter mViewPagerAdapter;

    private List<RtBean> mRtBeans;
    List<CTCheckData> ydata;

    private int mSelectPosition = 0;


    @Override
    protected boolean isRegisterEventBus() {
        return false;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_history;
    }

    @Override
    public void initViews() {

        ydata = new ArrayList<>();
        mUinv.setNavigationTitle("数据浏览");
        tvContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, HistorySearchActivity.class);
                startActivityForResult(intent, 999);
            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 999 && resultCode == RESULT_OK) {

            RtBean rtBean = (RtBean) data.getSerializableExtra(HistorySearchActivity.KEY_DATA);
            if (rtBean != null) {
                for (int i = 0; i < mRtBeans.size(); i++) {
                    if (mRtBeans.get(i).id == rtBean.id) {

                        int currentPosition = i;
                        mViewPager.setCurrentItem(currentPosition, false);

                        //设置按钮
                        if (currentPosition == 0) {
                            mBtnPre.setEnabled(false);
                            if (mRtBeans.size() > 1) {
                                mBtnNext.setEnabled(true);
                            } else {
                                mBtnNext.setEnabled(false);
                            }
                        } else if (currentPosition == mRtBeans.size() - 1) {

                            mBtnNext.setEnabled(false);
                            mBtnPre.setEnabled(true);

                        } else {
                            mBtnNext.setEnabled(true);
                            mBtnPre.setEnabled(true);
                        }

                        mTvNum.setText(rtBean.id + "");
                        mTvDate.setText(rtBean.date);

                        break;
                    }
                }
            }
        }
    }

    @Override
    public void loadData(Bundle savedInstanceState) {

        List<RtBean> product = SQLite.select()
                .from(RtBean.class)
                .queryList();
        if (product != null && product.size() > 0) {
            mLLNodata.setVisibility(View.GONE);
            mViewPager.setVisibility(View.VISIBLE);

            RtBean bean = product.get(0);
            mTvNum.setText(bean.id + "");
            if (product.size() > 1) {
                mBtnNext.setEnabled(true);
            } else {
                mBtnNext.setEnabled(false);
            }

            mTvDate.setText(bean.date);

        } else {
            mLLNodata.setVisibility(View.VISIBLE);
            mViewPager.setVisibility(View.INVISIBLE);
        }

        mRtBeans = new ArrayList<>();
        mRtBeans.addAll(product);
        mViewPagerAdapter = new ViewPagerAdapter(mRtBeans);
        mViewPager.setAdapter(mViewPagerAdapter);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {

                mSelectPosition = i;

                RtBean bean = mRtBeans.get(mSelectPosition);
                mTvNum.setText(bean.id + "");

                if (mSelectPosition == 0) {
                    mBtnPre.setEnabled(false);
                    if (mRtBeans.size() > 1) {
                        mBtnNext.setEnabled(true);
                    } else {
                        mBtnNext.setEnabled(false);
                    }
                } else if (mSelectPosition == mRtBeans.size() - 1) {

                    mBtnNext.setEnabled(false);
                    mBtnPre.setEnabled(true);

                } else {
                    mBtnNext.setEnabled(true);
                    mBtnPre.setEnabled(true);
                }

                mTvDate.setText(bean.date);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

    }

    @OnClick({R.id.btn_pre, R.id.btn_next, R.id.btn_delete, R.id.btn_delete_all, R.id.btn_exit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_pre:
                mViewPager.setCurrentItem(mSelectPosition - 1);
                break;
            case R.id.btn_next:
                mViewPager.setCurrentItem(mSelectPosition + 1);
                break;
            case R.id.btn_delete:
                new AlertDialog.Builder(mContext).
                        setTitle("提醒")
                        .setMessage("你确定要删除当前数据吗,删除后数据将不可恢复")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (mRtBeans == null || mRtBeans.size() == 0) {
                                    ToastUtils.showLong("没有数据可删除");
                                    return;
                                }

                                if (mSelectPosition == mRtBeans.size()) {
                                    mRtBeans.get(0).delete();
                                    mRtBeans.remove(0);
                                    finish();
                                    return;
                                }
                                //删除数据
                                mRtBeans.get(mSelectPosition).delete();
                                mRtBeans.remove(mSelectPosition);
                                mViewPagerAdapter = new ViewPagerAdapter(mRtBeans);
                                mViewPager.setAdapter(mViewPagerAdapter);

                                if (mRtBeans.size() > 0) {
                                    mLLNodata.setVisibility(View.GONE);
                                    mViewPager.setVisibility(View.VISIBLE);
                                    RtBean bean;
                                    int currentPosition;
                                    if (mSelectPosition - 1 >= 0) {

                                        currentPosition = mSelectPosition - 1;
                                        bean = mRtBeans.get(currentPosition);
                                        mViewPager.setCurrentItem(currentPosition, false);
                                    } else {
                                        currentPosition = 0;
                                        bean = mRtBeans.get(currentPosition);
                                        mViewPager.setCurrentItem(currentPosition, false);

                                    }

                                    //设置按钮
                                    if (currentPosition == 0) {
                                        mBtnPre.setEnabled(false);
                                        if (mRtBeans.size() > 1) {
                                            mBtnNext.setEnabled(true);
                                        } else {
                                            mBtnNext.setEnabled(false);
                                        }
                                    } else if (currentPosition == mRtBeans.size() - 1) {

                                        mBtnNext.setEnabled(false);
                                        mBtnPre.setEnabled(true);

                                    } else {
                                        mBtnNext.setEnabled(true);
                                        mBtnPre.setEnabled(true);
                                    }

                                    mTvNum.setText(bean.id + "");

                                    mTvDate.setText(bean.date);
                                } else {
                                    mLLNodata.setVisibility(View.VISIBLE);
                                    mViewPager.setVisibility(View.INVISIBLE);

                                    mTvNum.setText("");
                                    mTvDate.setText("");
                                }


                            }
                        }).
                        show();
                break;
            case R.id.btn_delete_all:
                new AlertDialog.Builder(mContext).
                        setTitle("提醒")
                        .setMessage("你确定要删除所有吗,删除后数据将不可恢复")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (mRtBeans == null || mRtBeans.size() == 0) {
                                    ToastUtils.showLong("没有数据可删除");
                                    return;
                                }

                                TipDialogUtils.showProgressDialog(mContext, "", true, new DialogInterface.OnCancelListener() {
                                    @Override
                                    public void onCancel(DialogInterface dialog) {

                                    }
                                });
                                FlowManager.getDatabase(AppDatabase.class)
                                        .beginTransactionAsync(new ProcessModelTransaction.Builder<>(
                                                new ProcessModelTransaction.ProcessModel<RtBean>() {
                                                    @Override
                                                    public void processModel(RtBean examModel, DatabaseWrapper wrapper) {
                                                        if (examModel != null) {
                                                            examModel.delete();
                                                        }
                                                    }

                                                }).addAll(mRtBeans).build())
                                        .error(new Transaction.Error() {
                                            @Override
                                            public void onError(Transaction transaction, Throwable error) {
                                                error.printStackTrace();
                                                TipDialogUtils.dismissProgressDialog();
                                                finish();
                                            }
                                        })
                                        .success(new Transaction.Success() {
                                            @Override
                                            public void onSuccess(Transaction transaction) {

                                                TipDialogUtils.dismissProgressDialog();
                                                finish();
                                            }
                                        }).build().execute();

                            }
                        }).
                        show();

                break;
            case R.id.btn_exit:
                finish();
                break;
        }
    }

    public class ViewPagerAdapter extends PagerAdapter {

        private static final String TAB1 = "tab1";
        private static final String TAB2 = "tab2";

        //ct
        TextView etTestCompany;
        TextView etMadeCompany;
        TextView etTaskNumber;
        TextView mEtPerson;
        TextView mDeviceNameTV;
        TextView mDeviceTypeTV;

        TextView mEtPropertyId;

        TextView mEtPosition;

        TextView mEtTem;
        TextView mEtHum;

        TextView mEtRatedElectricity;

        TextView mTvRatedElectricityTwo;

        TextView mEtRatedLoad;

        TextView mTvFloorLoad;

        TextView mTvLevel;

        TextView mTvCoso;

        TextView mTvF;

        TextView mTvU;

        TextView mTvI;

        TextView mTvR;

        TextView mTvResultKey;

        TextView mTvResult;

        LinearLayout mLlData;

        //pt

        TextView mDeviceNameTV2;
        TextView mDeviceTypeTV2;
        TextView mEtRatedLoadTwo;


        TextView mTvFloorLoadTwo;


        TextView mTvR1;

        TextView mTvX1;

        TextView mTvEmptyDifferBi;

        TextView mTvEmptyDifferJiao;

        TextView mTvFullDifferBi;

        TextView mTvFullDifferJiao;

        TextView mTvDifferBi;

        TextView mTvDifferJiao;


        TextView mTvG;

        TextView mTvB;

        TextView mTvU2;


        //第二步
        TextView etTestCompany2;
        TextView etMadeCompany2;
        TextView etTaskNumber2;
        TextView mEtPerson2;
        TextView mEtPropertyId2;
        TextView mEtPosition2;

        TextView mEtRatedElectricity2;
        TextView mTvRatedElectricityTwo2;

        TextView mEtRatedLoad2;
        TextView mTvFloorLoad2;

        TextView mEtRatedLoadTwo2;
        TextView mTvFloorLoadTwo2;

        TextView mTvLevel2;
        TextView mTvCoso2;

        public List<RtBean> mList;

        public ViewPagerAdapter(List<RtBean> view) {
            this.mList = view;

        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup view, int position, Object object) {
            view.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup view, int position) {

            RtBean bean = mList.get(position);
            View contentView = null;
            if (bean != null) {
                if (bean.type.equals(RtBean.TYPE_1)) {
                    contentView = LayoutInflater.from(mContext).inflate(R.layout.view_ct_pager, view, false);

                    etTestCompany = contentView.findViewById(R.id.et_test_company);
                    etMadeCompany = contentView.findViewById(R.id.et_made_company);
                    etTaskNumber = contentView.findViewById(R.id.et_task_number);
                    etTaskNumber = contentView.findViewById(R.id.et_task_number);
                    mDeviceNameTV = contentView.findViewById(R.id.device_name_tv);
                    mDeviceTypeTV = contentView.findViewById(R.id.device_type_tv);

                    mEtPerson = contentView.findViewById(R.id.et_person);

                    mEtPropertyId = contentView.findViewById(R.id.et_property_id);

                    mEtPosition = contentView.findViewById(R.id.et_position);

                    mEtTem = contentView.findViewById(R.id.et_tem);
                    mEtHum = contentView.findViewById(R.id.et_humidity);

                    mEtRatedElectricity = contentView.findViewById(R.id.et_rated_electricity);

                    mTvRatedElectricityTwo = contentView.findViewById(R.id.tv_rated_electricity_two);

                    mEtRatedLoad = contentView.findViewById(R.id.et_rated_load);

                    mTvFloorLoad = contentView.findViewById(R.id.tv_floor_load);

                    mTvLevel = contentView.findViewById(R.id.tv_level);

                    mTvCoso = contentView.findViewById(R.id.tv_coso);

                    mTvF = contentView.findViewById(R.id.tv_f);

                    mTvU = contentView.findViewById(R.id.tv_u);

                    mTvI = contentView.findViewById(R.id.tv_i);

                    mTvR = contentView.findViewById(R.id.tv_r);

                    mTvResultKey = contentView.findViewById(R.id.tv_result_key);

                    mTvResult = contentView.findViewById(R.id.tv_result);

                    mLlData = contentView.findViewById(R.id.ll_data);

                    String data = bean.TContent;
                    Gson gson = new Gson();
                    CurrentTransformerViewInfo viewInfo = gson.fromJson(data, CurrentTransformerViewInfo.class);
                    setViewInfoCT(viewInfo);
                    setisplayViewInfoCT(viewInfo);

                } else if (bean.type.equals(RtBean.TYPE_2)) {
                    contentView = LayoutInflater.from(mContext).inflate(R.layout.view_pt_pager, view, false);

                    etTestCompany = contentView.findViewById(R.id.et_test_company);
                    etMadeCompany = contentView.findViewById(R.id.et_made_company);
                    etTaskNumber = contentView.findViewById(R.id.et_task_number);
                    mDeviceNameTV = contentView.findViewById(R.id.device_name_tv);
                    mDeviceTypeTV = contentView.findViewById(R.id.device_type_tv);
                    mDeviceNameTV2 = contentView.findViewById(R.id.device_name_tv_2);
                    mDeviceTypeTV2 = contentView.findViewById(R.id.device_type_tv_2);

                    mEtPerson = contentView.findViewById(R.id.et_person);

                    mEtPropertyId = contentView.findViewById(R.id.et_property_id);

                    mEtPosition = contentView.findViewById(R.id.et_position);

                    mEtRatedElectricity = contentView.findViewById(R.id.et_rated_electricity);

                    mTvRatedElectricityTwo = contentView.findViewById(R.id.tv_rated_electricity_two);

                    mEtRatedLoad = contentView.findViewById(R.id.et_rated_load);

                    mTvFloorLoad = contentView.findViewById(R.id.tv_floor_load);

                    mTvLevel = contentView.findViewById(R.id.tv_level);

                    mTvCoso = contentView.findViewById(R.id.tv_coso);

                    mTvF = contentView.findViewById(R.id.tv_f);

                    mTvU = contentView.findViewById(R.id.tv_u);

                    mTvI = contentView.findViewById(R.id.tv_i);

                    mTvR = contentView.findViewById(R.id.tv_r);

                    mTvResultKey = contentView.findViewById(R.id.tv_result_key);

                    mTvResult = contentView.findViewById(R.id.tv_result);

                    mLlData = contentView.findViewById(R.id.ll_data);



                    mEtRatedLoadTwo = contentView.findViewById(R.id.et_rated_load_two);


                    mTvFloorLoadTwo = contentView.findViewById(R.id.tv_floor_load_two);


                    mTvR1 = contentView.findViewById(R.id.tv_r1);

                    mTvX1 = contentView.findViewById(R.id.tv_x1);

                    mTvEmptyDifferBi = contentView.findViewById(R.id.tv_empty_differ_bi);

                    mTvEmptyDifferJiao = contentView.findViewById(R.id.tv_empty_differ_jiao);

                    mTvFullDifferBi = contentView.findViewById(R.id.tv_full_differ_bi);

                    mTvFullDifferJiao = contentView.findViewById(R.id.tv_full_differ_jiao);

                    mTvDifferBi = contentView.findViewById(R.id.tv_differ_bi);

                    mTvDifferJiao = contentView.findViewById(R.id.tv_differ_jiao);


                    //第二部

                    etTestCompany2 = contentView.findViewById(R.id.et_test_company_2);
                    etMadeCompany2 = contentView.findViewById(R.id.et_made_company_2);
                    etTaskNumber2 = contentView.findViewById(R.id.et_task_number_2);
                    mEtPerson2 = contentView.findViewById(R.id.et_person_2);
                    mEtPropertyId2 = contentView.findViewById(R.id.et_property_id_2);
                    mEtPosition2 = contentView.findViewById(R.id.et_position_2);

                    mEtRatedElectricity2 = contentView.findViewById(R.id.et_rated_electricity_2);
                    mTvRatedElectricityTwo2 = contentView.findViewById(R.id.tv_rated_electricity_two_2);

                    mEtRatedLoad2 = contentView.findViewById(R.id.et_rated_load_2);
                    mTvFloorLoad2 = contentView.findViewById(R.id.tv_floor_load_2);

                    mEtRatedLoadTwo2 = contentView.findViewById(R.id.et_rated_load_two_2);
                    mTvFloorLoadTwo2 = contentView.findViewById(R.id.tv_floor_load_two_2);

                    mTvLevel2 = contentView.findViewById(R.id.tv_level_2);
                    mTvCoso2 = contentView.findViewById(R.id.tv_coso_2);


                    mTvG = contentView.findViewById(R.id.tv_g);

                    mTvB = contentView.findViewById(R.id.tv_b);

                    mTvU2 = contentView.findViewById(R.id.tv_u_2);


                    String data = bean.TContent;
                    Gson gson = new Gson();
                    VoltageTransformerViewInfo viewInfo = gson.fromJson(data, VoltageTransformerViewInfo.class);
                    setViewInfoPT(viewInfo);
                    setisplayViewInfoPT(viewInfo);
                }
            }


            view.addView(contentView);
            return contentView;
        }

        /**
         * ct
         *
         * @param info
         */
        private void setViewInfoCT(CurrentTransformerViewInfo info) {
            mEtRatedElectricity.setText(info.getMRatedPrimary());
            mTvRatedElectricityTwo.setText(info.getMRatedSecondary());
            mEtRatedLoad.setText(info.getMRatedLoad());
            mTvFloorLoad.setText(info.getMMiniLoad());
            mTvLevel.setText(info.getMGrade());
            mTvCoso.setText(info.getMPhaseAngle() + "");

            etTestCompany.setText(info.getTestCompany());
            etMadeCompany.setText(info.getMadeCompany());
            etTaskNumber.setText(info.getTaskNumber());
            mEtPerson.setText(info.getName());
            mEtPropertyId.setText(info.getNum());
            mEtPosition.setText(info.getAddress());
            mDeviceNameTV.setText(info.getDevciceName());
            mDeviceTypeTV.setText(info.getDeviceType());
            mEtPosition.setText(info.getAddress());
            mEtTem.setText(info.getTem());
            mEtHum.setText(info.getHum());
        }

        /**
         * ct
         *
         * @param info
         */
        private void setisplayViewInfoCT(CurrentTransformerViewInfo info) {

            if (ydata != null) {
                ydata.clear();
            }
            mTvR.setText(info.getSResult());
            mTvF.setText(info.getSPhaseError());
            mTvU.setText(info.getSWorkingVoltage());
            mTvI.setText(info.getSQuadratureError());
            mTvR.setText(info.getSSecondaryR());

            mTvResult.setText(info.getSResult());

            String[] iErrors = info.getSIErrors();
            String[] iRoundErrors = info.getSIRoundErrors();
            String[] sPhaseDisps = info.getSPhaseDisps();
            String[] sPhaseRoundDisps = info.getSPhaseRoundDisps();

//
//        float[] iErrors = new float[]{-0.21f, 0.111f, -0.086f, -0.026f, -0.033f, -0.03f, 0.016f, 0.004f, 0.027f};
//        float[] iRoundErrors = new float[]{-0.22f, -0.12f, -0.08f, -0.02f, -0.04f, 0f, 0f, 0f, 0.02f};
//        float[] sPhaseDisps = new float[]{8.6f, 10.3f, 5.8f, 1.1f, 1.1f, 4.6f, 7.5f, 5.0f, 1.6f};
//        float[] sPhaseRoundDisps = new float[]{1f, 10f, 6f, 1f, 1f, 5f, 7f, 5f, 2f};

            try {
                CTCheckData ctCheckDatay;
                for (int i = 0; i < iErrors.length; i++) {

                    ctCheckDatay = new CTCheckData();
                    ctCheckDatay.names = new ArrayList<>();
                    ctCheckDatay.names.add(iErrors[i] + "");
                    ctCheckDatay.names.add(iRoundErrors[i] + "");
                    ctCheckDatay.names.add(sPhaseDisps[i] + "");
                    ctCheckDatay.names.add(sPhaseRoundDisps[i] + "");
                    ydata.add(ctCheckDatay);
                }


                for (int i = 0; i < mLlData.getChildCount(); i++) {
                    //第一层viewgroup
                    ViewGroup view1 = (ViewGroup) mLlData.getChildAt(i);

                    //第二层viewgroup
                    for (int j = 0; j < view1.getChildCount(); j++) {
                        paseCtFormData(i, view1, j, info);

                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        private void paseCtFormData(int i, ViewGroup view1, int j, CurrentTransformerViewInfo viewInfo) {
            ViewGroup view2 = (ViewGroup) view1.getChildAt(j);

            int index = Math.min(8, i * 5 + j);
            CTCheckData data = ydata.get(index);
            //第三层view
            boolean isRedBg = false;
            for (int k = 0; k < view2.getChildCount(); k++) {
                if (view2.getChildAt(k).getId() == R.id.tv_result) {

                } else if (view2.getChildAt(k).getId() == R.id.tv_result_key) {

                } else {

                    if (index == 0 || index == 5) {
                        //精度等级为0.2S、0.5S时，需接收1%规程点的测试数据。其他等级时，需要把1%规程点的测试数据屏蔽掉（无数据）（数组1和数组6的数据）。
                        if (viewInfo.getmGrade() == 3 || viewInfo.getmGrade() == 4) {

                            String value = data.names.get(k);

                            if (!TextUtils.isEmpty(value)) {

                                float fValue = 0F;
                                try {
                                    fValue = Float.parseFloat(value);

                                } catch (NumberFormatException e) {
                                    e.printStackTrace();
                                }

                                float settingValueX = CheckData.ctMapXING.get(viewInfo.getmGrade())[j];
                                float settingValueB = CheckData.ctMapB.get(viewInfo.getmGrade())[j];
                                if (settingValueX == CheckData.UNKNOW_VALUE || settingValueB == CheckData.UNKNOW_VALUE) {

                                } else {
                                    if (k >= 2) {
                                        if (Math.abs(fValue) > settingValueX) {
                                            if (!isRedBg) {
                                                isRedBg = true;
                                            }
                                        }
                                    } else {
                                        if (Math.abs(fValue) > settingValueB) {
                                            if (!isRedBg) {
                                                isRedBg = true;
                                            }
                                        }
                                    }
                                }

                                ((TextView) view2.getChildAt(k)).setText(value);

                            } else {
                                ((TextView) view2.getChildAt(k)).setText("");
                            }

                        } else {

                            ((TextView) view2.getChildAt(k)).setText("");
                        }

                    } else {

                        String value = data.names.get(k);

                        if (!TextUtils.isEmpty(value)) {

                            float fValue = 0F;
                            try {
                                fValue = Float.parseFloat(value);

                            } catch (NumberFormatException e) {
                                e.printStackTrace();
                            }

                            float settingValueX = CheckData.ctMapXING.get(viewInfo.getmGrade())[j];
                            float settingValueB = CheckData.ctMapB.get(viewInfo.getmGrade())[j];
                            if (settingValueX == CheckData.UNKNOW_VALUE || settingValueB == CheckData.UNKNOW_VALUE) {

                            } else {
                                if (k >= 2) {
                                    if (Math.abs(fValue) > settingValueX) {
                                        if (!isRedBg) {
                                            isRedBg = true;
                                        }
                                    }
                                } else {
                                    if (Math.abs(fValue) > settingValueB) {
                                        if (!isRedBg) {
                                            isRedBg = true;
                                        }
                                    }
                                }
                            }

                            ((TextView) view2.getChildAt(k)).setText(value);

                        } else {
                            ((TextView) view2.getChildAt(k)).setText("");
                        }

                    }

                }

            }

            if (isRedBg) {
                for (int k = 0; k < view2.getChildCount(); k++) {
                    if (view2.getChildAt(k).getId() == R.id.tv_result) {

                    } else if (view2.getChildAt(k).getId() == R.id.tv_result_key) {

                    } else {
                        ((TextView) view2.getChildAt(k)).setBackgroundResource(R.drawable.verification_edit_bg_red);
                    }
                }
            } else {
                for (int k = 0; k < view2.getChildCount(); k++) {
                    if (view2.getChildAt(k).getId() == R.id.tv_result) {

                    } else if (view2.getChildAt(k).getId() == R.id.tv_result_key) {

                    } else {
                        ((TextView) view2.getChildAt(k)).setBackgroundResource(R.drawable.verification_edit_bg_normal);
                    }
                }
            }
        }

        /**
         * pt
         *
         * @param info
         */
        private void setViewInfoPT(VoltageTransformerViewInfo info) {

            //第一步
            mEtRatedElectricity.setText(info.getMRatedPrimary());
            mTvRatedElectricityTwo.setText(info.getMRatedSecondary());

            mEtRatedLoad.setText(info.getMMeaWinRatedLoad());
            mTvFloorLoad.setText(info.getMMeaWinMiniLoad());

            mEtRatedLoadTwo.setText(info.getMSurWinRatedLoad());
            mTvFloorLoadTwo.setText(info.getMSurWinMiniLoad());

            mTvLevel.setText(info.getMGrade());
            mTvCoso.setText(info.getMPhaseAngle() + "");

            etTestCompany.setText(info.getTestCompany());
            etMadeCompany.setText(info.getMadeCompany());
            etTaskNumber.setText(info.getTaskNumber());
            mDeviceNameTV.setText(info.getDeviceName());
            mDeviceTypeTV.setText(info.getDeviceType());
            mEtPerson.setText(info.getName());
            mEtPropertyId.setText(info.getNum());
            mEtPosition.setText(info.getAddress());


            //第二步
            etTestCompany2.setText(etTestCompany.getText().toString().trim());
            etMadeCompany2.setText(etMadeCompany.getText().toString().trim());
            etTaskNumber2.setText(etTaskNumber.getText().toString().trim());
            mDeviceNameTV2.setText(mDeviceNameTV.getText().toString().trim());
            mDeviceTypeTV2.setText(mDeviceTypeTV.getText().toString().trim());
            mEtPerson2.setText(mEtPerson.getText().toString().trim());
            mEtPropertyId2.setText(mEtPropertyId.getText().toString().trim());
            mEtPosition2.setText(mEtPosition.getText().toString().trim());

            mEtRatedElectricity2.setText(info.getMRatedPrimary());
            mTvRatedElectricityTwo2.setText(info.getMRatedSecondary());

            mEtRatedLoad2.setText(info.getMMeaWinRatedLoad());
            mTvFloorLoad2.setText(info.getMMeaWinMiniLoad());

            mEtRatedLoadTwo2.setText(info.getMSurWinRatedLoad());
            mTvFloorLoadTwo2.setText(info.getMSurWinMiniLoad());

            mTvLevel2.setText(info.getMGrade());
            mTvCoso2.setText(info.getMPhaseAngle() + "");

            mTvDate.setText(info.getTime());

        }

        /**
         * pt
         *
         * @param info
         */
        private void setisplayViewInfoPT(VoltageTransformerViewInfo info) {

            if (ydata != null) {
                ydata.clear();
            }
            //tab1 f u r
            mTvF.setText(info.getSPhaseError());
            mTvI.setText(info.getSQuadratureError());
            mTvU.setText(info.getSPrimaryU());
            mTvR.setText(info.getSSecondaryR());
            mTvR1.setText(info.getSDCR());
            mTvX1.setText(info.getSXb());

            //tab2 g n u
            mTvG.setText(info.getSG());
            mTvB.setText(info.getSB());
            mTvU2.setText(info.getSSecondaryU());

            mTvResult.setText(info.getSResult());

            //比差
            String[] sfErrors = info.getSFErrors();
            if (sfErrors.length == 3) {
                mTvEmptyDifferBi.setText(sfErrors[0]);
                mTvFullDifferBi.setText(sfErrors[1]);
                mTvDifferBi.setText(sfErrors[2]);
            }

            //角差
            String[] sdErrors = info.getSDErrors();

            if (sdErrors.length == 3) {
                mTvEmptyDifferJiao.setText(sdErrors[0]);
                mTvFullDifferJiao.setText(sdErrors[1]);
                mTvDifferJiao.setText(sdErrors[2]);
            }

            String[] iErrors = info.getSUErrors();
            String[] iRoundErrors = info.getSURoundErrors();
            String[] sPhaseDisps = info.getSPhaseDisps();
            String[] sPhaseRoundDisps = info.getSPhaseRoundDisps();

//
//        float[] iErrors = new float[]{-0.21f, 0.111f, -0.086f, -0.026f, -0.033f, -0.03f, 0.016f, 0.004f, 0.027f};
//        viewInfo.setsIErrors(iErrors);
//        float[] iRoundErrors = new float[]{-0.22f, -0.12f, -0.08f, -0.02f, -0.04f, 0f, 0f, 0f, 0.02f};
//        viewInfo.setsIRoundErrors(iRoundErrors);
//        float[] sPhaseDisps = new float[]{8.6f, 10.3f, 5.8f, 1.1f, 1.1f, 4.6f, 7.5f, 5.0f, 1.6f};
//        viewInfo.setsPhaseDisps(sPhaseDisps);
//        float[] sPhaseRoundDisps = new float[]{1f, 10f, 6f, 1f, 1f, 5f, 7f, 5f, 2f};
//        viewInfo.setsPhaseRoundDisps(sPhaseRoundDisps);

            try {
                CTCheckData ctCheckDatay;
                for (int i = 0; i < iErrors.length; i++) {

                    ctCheckDatay = new CTCheckData();
                    ctCheckDatay.names = new ArrayList<>();
                    ctCheckDatay.names.add(iErrors[i] + "");
                    ctCheckDatay.names.add(iRoundErrors[i] + "");
                    ctCheckDatay.names.add(sPhaseDisps[i] + "");
                    ctCheckDatay.names.add(sPhaseRoundDisps[i] + "");
                    ydata.add(ctCheckDatay);
                }


                for (int i = 0; i < mLlData.getChildCount(); i++) {
                    //第一层viewgroup
                    ViewGroup view1 = (ViewGroup) mLlData.getChildAt(i);

                    //第二层viewgroup
                    for (int j = 0; j < view1.getChildCount(); j++) {
                        pasePtFormData(i, view1, j, info);

                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

    private void pasePtFormData(int i, ViewGroup view1, int j, VoltageTransformerViewInfo viewInfo) {
        ViewGroup view2 = (ViewGroup) view1.getChildAt(j);

        int index = Math.min(8, i * 3 + j);
        CTCheckData data = ydata.get(index);
        //第三层view
        boolean isRedBg = false;
        for (int k = 0; k < view2.getChildCount(); k++) {
            if (view2.getChildAt(k).getId() == R.id.tv_result) {

            } else if (view2.getChildAt(k).getId() == R.id.tv_result_key) {

            } else {


                String value = data.names.get(k);

                if (!TextUtils.isEmpty(value)) {

                    float fValue = 0F;
                    try {
                        fValue = Float.parseFloat(value);

                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }

                    float settingValueX = CheckData.ptMapXING.get(viewInfo.getmGrade())[j];
                    float settingValueB = CheckData.ptMapB.get(viewInfo.getmGrade())[j];
                    if (settingValueX == CheckData.UNKNOW_VALUE || settingValueB == CheckData.UNKNOW_VALUE) {

                    } else {
                        if (k >= 2) {
                            if (Math.abs(fValue) > settingValueX) {
                                if (!isRedBg) {
                                    isRedBg = true;
                                }
                            }
                        } else {
                            if (Math.abs(fValue) > settingValueB) {
                                if (!isRedBg) {
                                    isRedBg = true;
                                }
                            }
                        }
                    }

                    ((TextView) view2.getChildAt(k)).setText(value);

                } else {
                    ((TextView) view2.getChildAt(k)).setText("");
                }

            }


        }

        if (isRedBg) {
            for (int k = 0; k < view2.getChildCount(); k++) {
                if (view2.getChildAt(k).getId() == R.id.tv_result) {

                } else if (view2.getChildAt(k).getId() == R.id.tv_result_key) {

                } else {
                    ((TextView) view2.getChildAt(k)).setBackgroundResource(R.drawable.verification_edit_bg_red);
                }
            }
        } else {
            for (int k = 0; k < view2.getChildCount(); k++) {
                if (view2.getChildAt(k).getId() == R.id.tv_result) {

                } else if (view2.getChildAt(k).getId() == R.id.tv_result_key) {

                } else {
                    ((TextView) view2.getChildAt(k)).setBackgroundResource(R.drawable.verification_edit_bg_normal);
                }
            }
        }
    }


}
